package com.dotflat.training.controller;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.training.domain.OTCourse;
import com.dotflat.training.po.OTCoursePO;
import com.dotflat.training.service.OTCourseService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/otcourse")
@Slf4j
@Api(tags = "线下培训课 - ")
public class OTCourseController extends RestfulBaseController<OTCourse, OTCourseService, OTCoursePO> {

    @Autowired
    public void setBaseService(OTCourseService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    public StatusResponse add(@RequestHeader(TICKET) String ticket, OTCoursePO param){
        StaffDTO staff = cache.getStaff(ticket);

        OTCourse ret = baseService.add(staff.getAreaId(), staff.getId(), param, o->{
            o.setStaffHeadImg(staff.getHeadImgUrl());
            o.setStaffName(staff.getRealName());
        });

        List<OTCourse> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }

    @ApiOperation(value = "修改课程状态，仅Status字段有用")
    @PostMapping("/update_status")
    public StatusResponse updateStatus(@RequestHeader(TICKET) String ticket, @RequestParam String id, @RequestParam Integer status){
        StaffDTO staff = cache.getStaff(ticket);

        OTCourse ret = baseService.get(staff.getAreaId(), staff.getId(), id);

        //0.预约中  1.签到中  2.已结束
        if (status == 0){
            ret.setStatusVal("预约中");
            ret.setStatus(0);
        }else if(status == 1){
            ret.setStatusVal("签到中");
            ret.setStatus(1);
        }else {
            ret.setStatusVal("已结束");
            ret.setStatus(2);
        }
        baseService.update(ret);

        return StatusResponse.success(ret);
    }

    @ApiOperation(value = "修改课程状态，仅Status字段有用")
    @PostMapping("/update_published")
    public StatusResponse updatePublished(@RequestHeader(TICKET) String ticket, @RequestParam String id, @RequestParam Boolean published){
        StaffDTO staff = cache.getStaff(ticket);

        OTCourse ret = baseService.get(staff.getAreaId(), staff.getId(), id);
        ret.setPublished(published);
        baseService.update(ret);

        return StatusResponse.success(ret);
    }
}
